Preview - Netlogo Investigations In Electromagnetism (Niels)

Plotting Instantaneous and Average Currents


Now you will learn how to graph average current in the model, just as instantaneous current is. The code below shows how instantaneous current was plotted in the model.

set-current-plot-pen provides a label for the graph and tells the model to start plotting something

plotxy ticks tells the model what to plot every tick (or time point)

current-now-list is a list that contains values representing how many charges are passing through this section of the wire at every tick for the past n ticks where n is the sampling rate in the slider

sum  adds up all the values in a list 

length finds the length of a list. For current-now-list, this will always be the sampling rate

Now, open the code tab of the model. To do this:

1) Scroll down in the model on the left and you will see three tabs "Command Center", "NetLogo Code", and "Model Info".

2) Click "NetLogo Code" to open the code tab. 

You can find the plotting code from above if you scroll down in the code tab to lines 378 and 379. Look in the code tab and find current-now-list. This shows how the current now (stored in current-now-list) is plotted in the graph within the model. 

If you want to see how current-now-list look at lines 204 and 205 which contain code that states if the length of the list is greater than the sampling-rate (that is set by you with the slider), the first item of the list is removed. So, if you set your sampling rate to 50, then my-charge-list shows how many charges pass through this sections of the wire for the most recent 50 ticks. You can see in lines 201 and 202 how the code adds the current at each tick to lists.  

With the model below, there is another list introduced called average-current-list which also shows how many charges are passing through this section of the wire at every tick like current-now-list, but average-current-list does not remove items when the length of the list surpasses the sampling rate. This means all values remain in the list and none are ever removed. 

Explore the model and look at the code tab to examine this.


Questions

Please answer the questions below.

At the bottom of the code tab, around line 371, you will see that the plotting code has been changed to reflect the addition of this new list, but the highlighted line, that should tell the plot what data to display, is still missing the code to create the plot. 

What code would you need to add on this highlighted line to make the plot appear correctly?

Hint: The code you wrote in Codap to perform this calculation similarly in a data table has a completely different syntax than NetLogo code. The NetLogo code should perform a similar operation, but you should use the other plotting code (line 369) as a reference for what your code should look like. 

Play around with the model to test your code. 


Notes

These notes will appear on every page in this lesson so feel free to put anything here you'd like to keep track of.